
import matplotlib.pyplot as plt
import matplotlib as mpl
import pandas as pd
import numpy as np
import math
def gra_fastApi(address):
    '''

    改进灰色关联系数 （有提升）

    2022年9月8日
    '''

    data = pd.read_csv(address, encoding='GBk', header=None)
    label_need = data.keys()[:]
    data1 = data[label_need].values
    [m, n] = data1.shape
    data2 = data1.astype('float')
    data3 = data2
    ymin = 0.002
    ymax = 1
    for j in range(0, n):
        d_max = max(data2[:, j])
        d_min = min(data2[:, j])
        data3[:, j] = (ymax - ymin) * (data2[:, j] - d_min) / (d_max - d_min) + ymin

    t = range(0, n - 1)

    yk = 0
    for l in range(0, m):
        # data3[:,n-1]=np.abs(data3[:,i]-data3[:,20])
        yk += data3[l:l + 1, n - 1]
    yk1 = yk / m
    # print(yk1)
    # print (data3[:,20])

    data5 = data3
    # data3[0:m,0]=np.abs(data3[0:m,0]-data3[0:m,n-1])   #计算一列的 x0(k)-xi(k)
    for i in range(0, n - 1):
        xk = 0
        for j in range(0, m):
            # print (data3[j:j + 1,0])
            xk += data3[j:j + 1, i]
        data3[:, i] = np.abs(np.sqrt(np.square(data3[:, i] - xk / m)) - np.sqrt(np.square(data3[:, n - 1] - yk1)))
        # print(data3[:, i])
    # print()

    data4 = data3[:, 0:n - 1]
    d_max = np.max(data4)
    d_min = np.min(data4)
    a = 0.5

    data4 = (d_min + a * d_max) / (data4 + a * d_max)
    res = np.mean(data4, axis=0)
    sorted_nums = sorted(enumerate(res), key=lambda x: x[1])
    idx = [i[0] for i in sorted_nums]
    nums = [i[1] for i in sorted_nums]
    # print(idx)
    return idx